package meijuli.com.join51.business.resetpwd

import android.annotation.SuppressLint
import android.app.Activity
import android.os.Bundle
import android.text.TextUtils
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.EditText
import meijuli.com.join51.business.R
import com.jhx.common.framework.QuickBaseFragment
import com.jhx.common.utils.ToastUtil
import kotlinx.android.synthetic.main.fragment_reset_pwd.*
import me.foji.anko.afterTextChanged
import org.jetbrains.anko.support.v4.toast

/**
 * 重置登录密码页面
 *
 * @author Jack 2018-01-20 16:22
 */
class ResetPwdFragment : QuickBaseFragment(), Contract.ResetPasswordView {

    private var mPresenter: Contract.ResetPasswordPresenter? = null
    /**
     * 密码规则，必须是数字和字母组合，并且在6-16位之间
     */
    private var regex = "^[0-9A-Za-z]{6,16}$"

    override fun bindView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        return inflater.inflate(R.layout.fragment_reset_pwd, container, false)
    }

    @SuppressLint("MissingSuperCall")
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        edit_old_pwd.afterTextChanged {
            btn_confirm.isEnabled = !checkInput()
        }

        edit_new_pwd.afterTextChanged {
            btn_confirm.isEnabled = !checkInput()
        }

        edit_confirm_pwd.afterTextChanged {
            btn_confirm.isEnabled = !checkInput()
        }

        btn_confirm.setOnClickListener {
            if(edit_new_pwd.text.toString() != edit_confirm_pwd.text.toString()) {
                toast("密码必须保持一致")
                return@setOnClickListener
            } else if (TextUtils.isEmpty(edit_new_pwd.text) || edit_new_pwd.text.contains(" ")
                    || edit_new_pwd.text.length < 6 || edit_new_pwd.text.length > 16) run {
                ToastUtil.getInstance().showToast("密码不能包含空格，并且在6-16位字符之间")
                return@setOnClickListener
            }
            mPresenter?.resetPwd(edit_old_pwd.text.toString(), edit_new_pwd.text.toString())
        }

        img_show_oldpwd.setOnClickListener {
            img_show_oldpwd.isSelected = !img_show_oldpwd.isSelected
            showPwd(img_show_oldpwd.isSelected, edit_old_pwd)
        }

        img_show_newpwd.setOnClickListener {
            img_show_newpwd.isSelected = !img_show_newpwd.isSelected
            showPwd(img_show_newpwd.isSelected, edit_new_pwd)
        }

        img_show_conpwd.setOnClickListener {
            img_show_conpwd.isSelected = !img_show_conpwd.isSelected
            showPwd(img_show_conpwd.isSelected, edit_confirm_pwd)
        }

        mPresenter = ResetPwdPresenterImpl()
        mPresenter?.setView(this)
    }

    private fun showPwd(isShow: Boolean, editText: EditText) {
        if(isShow){
            //如果选中，显示密码
            editText.transformationMethod = HideReturnsTransformationMethod.getInstance()
        }else{
            //否则隐藏密码
            editText.transformationMethod = PasswordTransformationMethod.getInstance()
        }
        editText.setSelection(editText.text.toString().length)
    }

    private fun checkInput(): Boolean {
        return TextUtils.isEmpty(edit_old_pwd.text)
               || TextUtils.isEmpty(edit_new_pwd.text)
               || TextUtils.isEmpty(edit_confirm_pwd.text)
    }

    override fun showLoading() {
        showLoadingDialog()
    }

    override fun hideLoading() {
        hideLoadingDialog()
    }

    override fun resetPwdSuccess() {
        pop()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mPresenter?.recycler()
    }

    override fun context(): Activity? {
        return activity
    }
}

